/*
 * FixedSizeBlockFile.h
 *
 *  Created on: Oct 10, 2012
 *      Author: m
 */

#ifndef FIXEDSIZEBLOCKFILE_H_
#define FIXEDSIZEBLOCKFILE_H_

#include <iostream>
#include <fstream>
#include "physical_layer_commons.h"

namespace physical
{

class FixedSizeBlockFile
{
private:
	std::fstream m_blockSource;
	size_t m_blockSize;
	static const openmode defaulOpenmode;

public:
	FixedSizeBlockFile( size_t blockSize, const char filename[] = NULL, openmode  mode = defaulOpenmode );
	virtual ~FixedSizeBlockFile();

	void readBlock( blockNumber position, byteStream buffer );
	void writeBlock( blockNumber position, const byteStream buffer );
	size_t getBlockSize();
	size_t getFileSize();
	size_t gcount();
	size_t getBlocksCount();
	bool eof();
	void open( const char filename[], openmode mode = defaulOpenmode );
	void close();
	void resetBlockSize( size_t blockSize );

private:
	void readBlock( byteStream buffer );
	void writeBlock( const byteStream buffer );
};

class FixedSizeBlockFileException : public PhysicalException
{};

class InvalidBlockSizeException: public FixedSizeBlockFileException
{
  virtual const char* what() const throw()
  {
    return "Trying I/O Operation from Block File with block size 0 or lower.";
  }
};

}
#endif /* FIXEDSIZEBLOCKFILE_H_ */
